package erbedo.call.tracker;

import java.util.Vector;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Trackall extends Activity {
	private static final int MENU_HELP = 0;
	private static final int MENU_ABOUT = 1;
	Vector<String> filters = new Vector<String>();
	ArrayAdapter<String> adapter;
	DBAdapter db;

	/** Called when the activity is first created. */
	@SuppressWarnings("unchecked")
	@Override
	public void onCreate(Bundle savedInstanceState) {

		db = new DBAdapter(this);
		db.open();

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		getAllFilters();

		adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, filters);
		ListView lv = (ListView)findViewById(R.id.list);
		lv.setAdapter(adapter);

		lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View view,	int position, long id) {
				try {
					Intent i = new Intent(getBaseContext(),Stats.class);
					i.putExtra("FILTER_ID", id);
					i.putExtra("FILTER_STRING",(String)arg0.getItemAtPosition(position));
					startActivity(i);
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		});

		lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(final AdapterView<?> arg0, View arg1, final int arg2, long arg3) {
				AlertDialog.Builder builder = new AlertDialog.Builder(Trackall.this);
				builder.setMessage("Delete this?")
				.setCancelable(false)
				.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						// Delete this id
						String toDelete = (String)arg0.getItemAtPosition(arg2);
						db.deleteFilter(toDelete);
						filters.clear();
						getAllFilters();
						// Refresh the content
						adapter.notifyDataSetChanged();
					}
				})
				.setNegativeButton("No", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				});
				AlertDialog alert = builder.create();

				//				try {
				alert.show();
				////				} catch (Exception e) {
				//					
				//				}
				return false;
			}

		});

		Button b = (Button)findViewById(R.id.addit);
		b.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				EditText et = (EditText)findViewById(R.id.newfilter);
				String text = et.getText().toString();
				db.insertFilter(text);
				Toast.makeText(getBaseContext(), "Filter Added", Toast.LENGTH_LONG).show();
				try {
					filters.add(text);
					adapter.notifyDataSetChanged();
					et.setText("");
				} catch (Exception e) {
					Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show();
				}
			}
		});
	}

	/* Creates the menu items */
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, MENU_HELP, 0, "Help");
		menu.add(0, MENU_ABOUT, 0, "About");
		return true;
	}

	private void getAllFilters() {
		filters.add("ALL CALLS");
		Cursor c = db.getAllFilters();
		startManagingCursor(c);
		c.moveToFirst();
		if (!(c.getCount() == 0)) {
			while (!c.isLast()) {
				Toast.makeText(this, c.getString(c.getColumnIndex("filter")), Toast.LENGTH_SHORT).show();
				filters.add(c.getString(c.getColumnIndex("filter")));
				c.moveToNext();
			}
			filters.add(c.getString(c.getColumnIndex("filter")));
		}
	}

	/* Handles item selections */
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case MENU_HELP:
			Dialog d = new Dialog(this);
			d.setTitle("Help");
			d.setContentView(R.layout.dialog_help);
			TextView tv = (TextView)d.findViewById(R.id.dialog_help_text);
			tv.setText("To add a filter, simply put the number on the box below and click the add button. \n\n" +
					"If you put a 1, then you'll match all the numbers that contains 1.\n\n" +
					"If you put 123, then you'll match all the numbers that contains 123 and so on.\n\n" +
			"More features coming, click on the about menu button to see what's going on.");
			d.show();
			return true;

		case MENU_ABOUT:
			Dialog a = new Dialog(this);
			a.setTitle("About");
			a.setContentView(R.layout.dialog_help);
			TextView about = (TextView)a.findViewById(R.id.dialog_help_text);
			about.setText("Trackall version 0.1 beta.\n\n" +
					"If you find any error, please report them as a comment and i'll work to fix them out.\n\n" +
			"Developed by Luca Bedogni.");
			a.show();
			return true;
		}
		return false;
	}

}